import axios from '../config/axiosConfig'; // 引入封装的 axios

/**
 * 优惠券接口返回类型
 */
export interface Coupon {
	id : number;
	name : string;
	description ?: string;
	type : number; // 1: 满减券, 2: 折扣券
	condition : number; // 1: 无门槛, 2: 满金额可用
	value : number;
	minAmount ?: number;
	maxAmount ?: number;
	startDate : string;
	endDate : string;
	status : number; // 1: 未使用, 2: 已使用, 3: 已过期
	createdAt : string;
	updatedAt : string;
	couponGoods ?: any[];
}

/**
 * 表单配置接口类型
 */
interface AddFormDataFace {
	name : string;
	type : string;
	value : string;
	goods : number[];
	condition : number;
	minAmount : string;
	maxAmount : string;
	startDate : string;
	endDate : string;
	description : string;
}

export interface ButtonData {
	label : string;
	type : string;
	value : string;
}

interface TableConfigDataFace {
	border : boolean;
	data : Coupon[];
	columns : {
		label : string;
		prop ?: string;
		align : string;
		type ?: string;
		width ?: string;
		button ?: ButtonData[];
	}[];
}

/**
 * 添加优惠券表单格式
 */
export const addFormData : AddFormDataFace = {
	name: '',
	type: '',
	value: '',
	goods: [],
	condition: '',
	minAmount: '',
	maxAmount: '',
	startDate: '',
	endDate: '',
	description: ''
}
/**
 * 添加优惠券规则
 */
export const addFormRules = {
	name: [
		{ required: true, message: '请输入优惠券名称', trigger: 'blur' }
	],
	type: [
		{ required: true, message: '请选择优惠券类型', trigger: 'change' }
	],
	value: [
		{ required: true, message: '请输入优惠券面值', trigger: 'blur' }
	],
	condition: [
		{ required: true, message: '请选择优惠券使用条件', trigger: 'change' }
	],
	minAmount: [
		{ required: true, message: '请输入最低消费金额', trigger: 'blur' }
	],
	maxAmount: [
		{ required: true, message: '请输入最高优惠金额', trigger: 'blur' }
	],
	startDate: [
		{ required: true, message: '请选择优惠券开始时间', trigger: 'change' }
	],
	endDate: [
		{ required: true, message: '请选择优惠券结束时间', trigger: 'change' }
	]
}

/**
 * 表格配置
 */
export const tableConfigData : TableConfigDataFace = {
	border: false,
	data: [],
	columns: [
		{ label: 'ID', prop: 'id', align: 'center' },
		{ label: '优惠券名称', prop: 'name', align: 'center' },
		{ label: '优惠券类型', prop: 'typeText', align: 'center' },
		{ label: '优惠券金额', prop: 'valueText', align: 'center' },
		{ label: '使用条件', prop: 'conditionText', align: 'center' },
		{ label:'开始时间', prop:'startDateText', align:'center' },
		{ label:'结束时间', prop:'endDateText', align:'center' },
		{ label: '创建时间', prop: 'createdAt', align: 'center' },
		{
			label: '操作',
			align: 'center',
			type: 'button',
			width: '200px',
			button: [
				{ label: '编辑', type: 'primary', value: 'edit' },
				{ label: '删除', type: 'danger', value: 'delete' },
			],
		},
	],
};

/**
 * formData 搜索表单
 */
export const formDataInfo = {
	inline: true,
	fields: [{
		label: '优惠券名称',
		type: 'input',
		name: 'name',
		value: '',
		placeholder: '请输入优惠券名称搜索',
		width: '350px'
	}, {
		label: '优惠券类型',
		type: 'select',
		name: 'type',
		value: '',
		placeholder: '请选择',
		options: [{
			label: '满减券',
			value: 1
		}, {
			label: '折扣券',
			value: 2
		}]
	}]
};

/**
 * 获取优惠券列表
 */
export const getCouponsApi = (data ?: { page ?: number; pageSize ?: number }) => {
	return axios.get('/coupons', { params: data });
};

/**
 * 创建优惠券
 */
export const createCouponApi = (data : AddFormDataFace) => {
	return axios.post('/coupons', data);
};

/**
 * 更新优惠券
 */
export const updateCouponApi = (
	id : number,
	data : AddFormDataFace
):any => {
	return axios.put(`/coupons/${id}`, data);
};

/**
 * 删除优惠券
 */
export const deleteCouponApi = (id : number) => {
	return axios.delete(`/coupons/${id}`);
};

/**
 * 获取单个优惠券详情
 */
export const getCouponByIdApi = (id : number) => {
	return axios.get(`/coupons/${id}`);
};